package ie.deloitte.cvapp.validators;



import java.util.Date;

import ie.deloitte.cvapp.domain.Employee;
import ie.deloitte.cvapp.form.EmployeeBeanForm;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class EmployeeValidator implements Validator {

	//@Override
	public boolean supports(Class<?> clazz) {
		return Employee.class.isAssignableFrom(clazz);
	}
	
	//@Override
	public void validate(Object target, Errors errors) {
		//ValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","name.required");
	    //ValidationUtils.rejectIfEmptyOrWhitespace(errors,"surname","name.required");
	    
		EmployeeBeanForm employeeBeanForm = (EmployeeBeanForm) target;
		

	    if ((employeeBeanForm.getName()).length()<2) {
	    	errors.rejectValue("name","name.lengthRequired");
	    }
	    
	    if ((employeeBeanForm.getSurname()).length()<2) {
	    	errors.rejectValue("surname","surname.lengthRequired");
	    }
	    
	    String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	    String emailAddress = employeeBeanForm.getEmail();
	    if (!emailAddress.matches(emailPattern)) {
	    	errors.rejectValue("email","email.incorrectFormat");
	    }
	    
	    if ((employeeBeanForm.getPrimarytelephone()).isEmpty()) {
	    	errors.rejectValue("primarytelephone","primarytelephone.required");
	    }
	    	
	 }

}